package com.kly.chatter.api.application.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import com.kly.sc.FirebaseTest;
import com.kly.sc.api.application.annos.SignIgnore;
import com.kly.sc.api.application.annos.VisitorAccessible;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;

@RestController
@RequestMapping("test")
public class TestController {


    @NacosValue(value = "${app.configs.appConfig.upGradationDesc}", autoRefreshed = true)
    private String upGradationDesc;

    @RequestMapping("/01")
    @SignIgnore
    @VisitorAccessible
    @SentinelResource
    public String test01(){

        String token="cmIrGneJTIebusoQgmgiwu:APA91bFD6wC0APaAmW7B_7DGezvMgxT7rZBezngkCAPk2Ig7R9fg7Cx1nFn7gqe355UeyD1__05X90t4Cr84tX2gxb1x1LvhwmhebKF6z_jzFuG4FoHGXYWsJ2FUqRTnJF3YyPGdi4gc";
        try {
            InputStream serviceAccount = this.getClass().getClassLoader().getResourceAsStream("meboo-online.json");
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .build();

            FirebaseApp.initializeApp(options);
            System.out.println("Firebase service account key is valid.");

            Notification notification = new Notification("1","[\"1\",\"2\"]");

            // Build a message
            Message message = Message.builder()
                    .setNotification(notification)
                    .setToken(token)
                    .build();

            // Send the message
            String response = FirebaseMessaging.getInstance().sendAsync(message).get();
            System.out.println("Sent message: " + response);
            return response;
        } catch (Exception e) {
            System.out.println("Error initializing Firebase: " + e.getMessage());
            return e.getMessage();
        }

    }

    @RequestMapping("/02")
    @SignIgnore
    @VisitorAccessible
    @SentinelResource("HelloWorld")
    public String test02(){
        return upGradationDesc;
    }
}
